package com.simpleframe.core.net;

import java.util.HashMap;
import java.util.Map;

import com.simpleframe.util.StringUtil;

public class URLParser {
	/** 需要解析的URL */
//	private String url;
//	
//	/** 协议 也就是httl或ftp */
//	private String protocol;
//	
//	/** www.xxx.com */
//	private String authority;
//	
//	/** 端口 */
//	private String port;
//	
//	/** \xxx\xxxx\xxxx.jsp */
//	private String path;
	
	//属全新的所有字符
	private String header;
	
	/** xxx=xxx&xxxxx=xxx */
	private String query;
	
	/** xx\sss\sss.jsp?xxx=xxx&sdss=sds */
	//private String file;
	
	/** URL对应的属性 */
	private Map<String, String> parameters;
	
	/** ref 也就是#后面的值 */
	private String ref;
	
	public URLParser(String url){
		//this.url = url;
		parseHeader(url);
		parseQuery(url);
		parseRef(url);
		parseParameters();//解析属性
	}
	
	private void parseHeader(String url){
		if(StringUtil.containChar(url, "?")){
			header = url.substring(0, url.indexOf("?"));
		}else if(StringUtil.containChar(url, "#")){
			header = url.substring(0,url.indexOf("#"));
		}else{
			header = url;
		}
	}
	
	private void parseQuery(String url){
		if(StringUtil.containChar(url, "?")){
			if(StringUtil.containChar(url, "#")){
				query = url.substring(url.indexOf("?")+1, url.indexOf("#"));
			}else{
				query = url.substring(url.indexOf("?")+1, url.length());
			}
		}
	}
	
	private void parseRef(String url){
		if(StringUtil.containChar(url, "#")){
			ref = url.substring(url.indexOf("#"),url.length());
		}
	}
	
	private void parseParameters(){
		if(StringUtil.hasLength(query)){
			String[] param = query.split("&");
			for (int i = 0; i < param.length; i++) {
				parseParamters(param[i]);
			}
		}
	}
	
	private void parseParamters(String param){
		if(StringUtil.containChar(param, "=")){
			String[] arr = param.split("=");
			setParameter(arr[0], arr[1]);
		}else{
			setParameter(param, "");
		}
	}
	
	public void setParameter(String name, String value){
		if(parameters==null){
			parameters = new HashMap<String,String>();
		}
		parameters.put(name, value);
	}
	
	public void deleteParameter(String name){
		if(parameters.containsKey(name)){
			parameters.remove(name);
		}
	}
	
	private String constructParameter(){
		StringBuffer urlBuffer = new StringBuffer();
		for (String name:parameters.keySet()) {
			//System.out.println(name);
			urlBuffer.append(name);
			urlBuffer.append("=");
			urlBuffer.append(parameters.get(name));
			urlBuffer.append("&");
		}
		urlBuffer.delete(urlBuffer.lastIndexOf("&"), urlBuffer.length());
		return urlBuffer.toString();
	}
	
	public String getURL(){
		StringBuffer urlBuffer = new StringBuffer();
		urlBuffer.append(header);
		//System.out.println(constructParameter());
		if(parameters.size() > 0){
			urlBuffer.append("?");
			urlBuffer.append(constructParameter());
		}
		if(ref != null){
			urlBuffer.append(ref);
		}
		return urlBuffer.toString();
	}
	
	public static void main(String[] args) {
		URLParser up = new URLParser("user.dsp");
		up.setParameter("userName", "");
		up.setParameter("useame", "");
		//up.setParameter("userName", "cccc");
		System.out.println(up.getURL());
	}
}
